<template>
  <div v-if="isVisible" class="modal">
    <div class="modal-content" :style="{ height: contentHeight }">
      <div class="message-container">
        <p>{{ message }}</p>
      </div>
      <button class="confirm-button" @click="confirm">确认</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isVisible: false,
      message: '',
      resolvePromise: null,
    };
  },
  computed: {
    contentHeight() {
      const widthPercentage = 30; // 30%
      const heightPercentage = 0.5; // 高度与宽度的比值
      return `${(window.innerWidth * widthPercentage / 100) * heightPercentage}px`;
    },
  },
  methods: {
    open(message) {
      this.message = message;
      this.isVisible = true;

      return new Promise((resolve) => {
        this.resolvePromise = resolve;
      });
    },
    close() {
      this.isVisible = false;
    },
    confirm() {
      this.close();
      if (this.resolvePromise) {
        this.resolvePromise(); // 触发 Promise
      }
    },
  },
};
</script>

<style scoped>
.modal {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 5px;
  width: 30%; /* 设置宽度为 30% */
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column; /* 使用列方向排列 */
  justify-content: center; /* 垂直居中 */
  align-items: center; /* 水平居中 */
  text-align: center; /* 文本居中 */
}

.message-container {
  flex-grow: 1; /* 使信息容器占据剩余空间 */
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
}

.confirm-button {
  background-color: #152ca8; /* 按钮颜色 */
  color: white;
  border: none;
  padding: 5px 10px; /* 减小按钮的上下和左右内边距 */
  border-radius: 5px;
  cursor: pointer;
  margin-top: 10px; /* 按钮与信息间隔 */
  width: 30%;
}

.confirm-button:hover {
  background-color: #0056b3; /* 按钮悬停颜色 */
}
</style>
